package com.jk.service.impl;

import com.jk.bean.Article;
import com.jk.bean.BiaoTi;
import com.jk.bean.History;
import com.jk.bean.Type;
import com.jk.mapper.HistoryMapper;
import com.jk.service.HistoryService;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Resource
    private HistoryMapper historyMapper;

    @Resource
     private MongoTemplate mongoTemplate;

    @Override
    public List<History> getHistory() {

        return historyMapper.getHistory();

    }

    @Override
    public List<Type> getPage() {
        return historyMapper.getPage();
    }

    @Override
    public List<BiaoTi> queryBiaoti(String biaoti) {


        return historyMapper.queryBiaoti(biaoti);
    }

    @Override
    public List<BiaoTi> getOne(String id) {
        List<BiaoTi> jk=historyMapper.getOne(id);
        List<BiaoTi> ba = new ArrayList<>();
        for (BiaoTi biaoTi : jk) {
            ba.add(biaoTi);
            if(ba.size()>=10){
              break;
            }
        }
        return ba;
    }

    @Override
    public List<BiaoTi> getBoWen() {
        List<BiaoTi> tb=historyMapper.getBoWen();
        ArrayList<BiaoTi> kg = new ArrayList<>();
        for (BiaoTi biaoTi : tb) {
            kg.add(biaoTi);
            if(kg.size()>=10){
              break;
            }
        }

        return kg;
    }

    @Override
    public List<BiaoTi> getBoZhu() {
        List<BiaoTi> list=historyMapper.getBoZhu();

        ArrayList<BiaoTi> ts = new ArrayList<>();

        for (BiaoTi bt : list) {
            ts.add(bt);
            if(ts.size()>=10){
              break;
            }
        }

        return ts;
    }

    @Override
    public List<Article> getText() {
        Query query = new Query();

        List<Article> li = mongoTemplate.find(query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "ditime"))), Article.class);
        List<Article> po = new ArrayList<>();

        for (Article article : li) {
            po.add(article);
            if (po.size() >= 3) {
                break;
            }
        }
        return po;

    }
}
